local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHOLYBUIBase());
local CallBackManager = ClientOHOH.load.CallBackManager();
local BtnHelper = ClientOHOH.load.BtnHelper();
local LanguageHelper=_G.LanguageHelper 
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();
local ConnectClass=_G.CommonOHOH.load.Connect();
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local UIManager = ClientOHOH.load.UIManager();
function InitInsts(ui)
	ui.insts = {};
    --控制homeUI的显示（不包括药水的显示）
	ui.insts.uiBtn ="ctrl.UI.UI"
    ui.insts.mainFrame = "ctrl.UI.show"
    ui.insts.setting = "ctrl.setting"

    --左侧按钮
    ui.insts.carBtn = "ctrl.btn.car.btn"
    ui.insts.carRedDot = "ctrl.btn.car.red"
    ui.insts.carRedNum = "ctrl.btn.car.redNum"

    ui.insts.potionBtn = "ctrl.btn.potion.btn"
    ui.insts.potionDot = "ctrl.btn.potion.red"
    ui.insts.potionRedNum = "ctrl.btn.potion.redNum"

    ui.insts.questsBtn = "ctrl.btn.quests.btn"
    ui.insts.questsDot = "ctrl.btn.quests.red"
    ui.insts.questsRedNum = "ctrl.btn.quests.redNum"

    ui.insts.pvpBtn = "ctrl.btn.pvp.btn"
    ui.insts.shopBtn = "ctrl.btn.shop.btn"

    ui.insts.diamondBtn = "ctrl.diamond.btn"
    ui.insts.diamondNum = "ctrl.diamond.num"

    --下侧按钮
    ui.insts.gamepass = {
        doubleRoll = "ctrl.gamepass.doubleRoll",
        luck2 = "ctrl.gamepass.luckRoll",
        luck4 = "ctrl.gamepass.superLuckyRoll",
        rollSpeed = "ctrl.gamepass.quickRollSpeed"
    }
   

    ui.insts.inviteBtn = "ctrl.inviteFriends.btn"
    ui.insts.inviteText = "ctrl.inviteFriends.text"
    --药水
    ui.insts.potionRoot = "ctrl.potion.root"
    ui.insts.potionCell = "ctrl.potion.cell"

    --右侧information
    ui.insts.info = {
        luck = "ctrl.information.Luck",
        rainbowLuck = "ctrl.information.rainbowLuck",
        rollSpeed = "ctrl.information.rollSpeed",
        shinyLuck = "ctrl.information.shinyLuck",
        voidLuck = "ctrl.information.voidLuck"
    }
  

end


function M.new(...)
	return M(...);
end;

function M.ctor(self)
	InitInsts(self)
	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.HomeGui"));
    self.potionList = {}
    self.buyEvent = ConnectClass.new()
    self:SetPotionRed()
    self:SetCarRed()
    self:SetQuestRed()
    self:AddConnect()
end;

function M.AddConnect(self)
    --ui按钮
    CallBackManager:AddRbxConnect(self,self.insts.uiBtn.Activated,function()
        self.insts.mainFrame.Visible = not self.insts.mainFrame.Visible
	end)

    --设置
    CallBackManager:AddRbxConnect(self,self.insts.setting.Activated,function()
        if UIManager.ui_list.SettingGui.main_inst.Enabled then
            UIManager.ui_list.SettingGui:Hide()
        else
            UIManager.ui_list.SettingGui:Show()
        end
	end);

    --任务
    CallBackManager:AddRbxConnect(self,self.insts.questsBtn.Activated,function()
        if UIManager.ui_list.TaskGui.main_inst.Enabled then
            UIManager.ui_list.TaskGui:Hide()
        else
            UIManager.ui_list.TaskGui:Show()
            self:SetQuestRed()
        end
		
      
	end);

    --药水
    CallBackManager:AddRbxConnect(self,self.insts.potionBtn.Activated,function()
        if UIManager.ui_list.PotionsGui.main_inst.Enabled then
            UIManager.ui_list.PotionsGui:Hide()
        else
            UIManager.ui_list.PotionsGui:Show()
            self:SetPotionRed()
        end
	end);

    
    --PVP
    CallBackManager:AddRbxConnect(self,self.insts.pvpBtn.Activated,function()
        if UIManager.ui_list.PVPGui.main_inst.Enabled then
            UIManager.ui_list.PVPGui:Hide()
        else
            UIManager.ui_list.PVPGui:Show()
        end
	end);

    
    --商店
    CallBackManager:AddRbxConnect(self,self.insts.shopBtn.Activated,function()
        if UIManager.ui_list.ShopGui.main_inst.Enabled then
            UIManager.ui_list.ShopGui:Hide()
        else
            UIManager.ui_list.ShopGui:Show()
        end
	end);


    --car仓库
    CallBackManager:AddRbxConnect(self,self.insts.carBtn.Activated,function()
        if UIManager.ui_list.CarStoreGui.main_inst.Enabled then
            UIManager.ui_list.CarStoreGui:Hide()
        else
            UIManager.ui_list.CarStoreGui:Show()
            self:SetPotionRed()
        end
	end);

    
    --邀请好友
    -- CallBackManager:AddRbxConnect(self,self.insts.inviteBtn.Activated,function()
    --     if UIManager.ui_list.InviteFriendGui.main_inst.Enabled then
    --         UIManager.ui_list.InviteFriendGui:Hide()
    --     else
    --         UIManager.ui_list.InviteFriendGui:Show()
    --     end
	-- end);


     --钻石
     CallBackManager:AddRbxConnect(self,self.insts.diamondBtn.Activated,function()
        if UIManager.ui_list.ShopGui.main_inst.Enabled then
                UIManager.ui_list.ShopGui:Go("diamond")
        else
            self:AddProcess(function()
                UIManager.ui_list.ShopGui:Show()
                self:Wait(0.1)
                UIManager.ui_list.ShopGui:Go("diamond")
            end)
        end
	end);


    --gamepass luck2
    CallBackManager:AddRbxConnect(self,self.insts.gamepass.luck2.Activated,function()
        self.buyEvent:Action(self.data["luck2"].gid)
	end);

    --gamepass luck4
    CallBackManager:AddRbxConnect(self,self.insts.gamepass.luck4.Activated,function()
        self.buyEvent:Action(self.data["luck4"].gid)
	end);


    --gamepass rollSpeed
    CallBackManager:AddRbxConnect(self,self.insts.gamepass.rollSpeed.Activated,function()
        self.buyEvent:Action(self.data["quickRollSpeed"].gid)
	end);

      --gamepass doubleRoll
      CallBackManager:AddRbxConnect(self,self.insts.gamepass.doubleRoll.Activated,function()
        self.buyEvent:Action(self.data["doubleRoll"].gid)
	end);

   

end


--药水cell
local cellStr = {
    icon = "ctrl.icon",
    time = "ctrl.time"
}
--创建药水cell
function M.CreatePotionCell(self,icon,time,id)
    local potionObj = self.insts.potionCell:Clone()
    local potion = self:InitUiInst2(potionObj,cellStr)
    self:InitUiObj(potionObj)
    potionObj.Parent = self.insts.potionRoot
    potionObj.Visible = true
    potion.icon.Image = icon
    potion.obj = potionObj
    potion.id = id
    potion.leftTime = 0
    potion.connect = self:CountDown(potion,time)
    return potion;
end

--药水使用后倒计时
function M.CountDown(self,potion,time)
    local connect = nil
    connect = self:AddProcess(function()
        for i = time, 1, -1 do
            potion.leftTime = i
            potion.time.Text = GeneralTool.secondsToHHMM(i)
            self:Wait(1)
        end
        potion.obj:Destroy()
        potion = nil
    end)
    return connect;
end


-------------------外部接口---------------
--设置右侧数值详情
function M.SetInfo(self,data)
    self.insts.info.luck.Text = data.Lucky
    self.insts.info.rainbowLuck.Text =  BigNumberHelper:ToString(data.RainbowLuck*100) .."%"
    self.insts.info.shinyLuck.Text =  BigNumberHelper:ToString(data.ShinyLuck*100) .."%"
    self.insts.info.voidLuck.Text = BigNumberHelper:ToString(data.VoidLuck*100) .."%"
    self.insts.info.rollSpeed.Text = BigNumberHelper:ToString(data.RollSpeed*100) .."%"
    self.insts.diamondNum.Text = BigNumberHelper:ToString(data.diamond)
end

--设置药水图标红点
function M.SetPotionRed(self,count)	
	if not count or UIManager.ui_list.PotionsGui.main_inst.Enabled or count == 0 then
		self.insts.potionRedNum.Text="0"
		self.insts.potionDot.Visible=false
	else
		self.insts.potionDot.Visible=true
		local oldCount=tonumber(self.insts.potionRedNum.Text)
		self.insts.potionRedNum.Text=tostring(count+oldCount) 
	end
end

--设置汽车仓库的红点
function M.SetCarRed(self,count)
	if not count or UIManager.ui_list.CarStoreGui.main_inst.Enabled then
		self.insts.carRedNum.Text="0"
		self.insts.carRedDot.Visible=false
	else
		self.insts.carRedDot.Visible=true
		local oldCount=tonumber(self.insts.carRedNum.Text)
		self.insts.carRedNum.Text=tostring(count+oldCount) 
	end
end

--设置任务的红点
function M.SetQuestRed(self,count)	
	if not count or UIManager.ui_list.TaskGui.main_inst.Enabled then
		self.insts.questsRedNum.Text="0"
		self.insts.questsDot.Visible=false
	else
		self.insts.questsDot.Visible=true
		local oldCount=tonumber(self.insts.questsRedNum.Text)
		self.insts.questsRedNum.Text=tostring(count+oldCount) 
	end
end

--更新gamepass状态
function M.UpdateGamepass(self,id)
    local num = 0
    if id == self.data["luck2"].gName then
        self.insts.gamepass.luck2.Visible = false
        num=  1
    elseif id == self.data["luck4"].gName then
        self.insts.gamepass.luck4.Visible = false
        num=  1
    elseif id == self.data["quickRollSpeed"].gName then
        self.insts.gamepass.rollSpeed.Visible = false
        num=  1
    elseif id ==self.data["doubleRoll"].gName then
        self.insts.gamepass.doubleRoll.Visible = false
        num=  1
    end
    return num
end

--设置主界面售卖的gamepass数据
function M.SetGamepassData(self,data)
    self.data = data
end

function M.dtor(self)
	M.super.dtor(self);
end;



return M
